package com.four.service.impl;

import com.four.entity.Admin;
import com.four.entity.News;
import com.four.mapper.NewsMapper;
import com.four.service.NewsService;
import com.four.util.WebUtil;
import com.four.vo.NewsSearchForm;
import com.four.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: WangPeng
 * @createTime: 2022/10/17 18:10
 * @description:
 * @version: v1.0
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;


    @Override
    public Result selectAllAddnews9And1(Integer currentPage, Integer pageSize, NewsSearchForm newsSearchForm) {
        PageHelper.startPage(currentPage,pageSize);
        List<News> news = newsMapper.selectAllAddNews9And1(newsSearchForm.getTitle(),newsSearchForm.getKind(),newsSearchForm.getAuthor());
        PageInfo<News> pageInfo = new PageInfo<>(news);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result selectAllAddnews9(Integer currentPage, Integer pageSize, NewsSearchForm newsSearchForm) {
        PageHelper.startPage(currentPage,pageSize);
        List<News> news = newsMapper.selectAllAddNews9(newsSearchForm.getTitle(),newsSearchForm.getKind(),newsSearchForm.getAuthor());
        PageInfo<News> pageInfo = new PageInfo<>(news);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result selectAllAddnews1(Integer currentPage, Integer pageSize, NewsSearchForm newsSearchForm) {
        PageHelper.startPage(currentPage,pageSize);
        List<News> news = newsMapper.selectAllAddNews1(newsSearchForm.getTitle(),newsSearchForm.getKind(),newsSearchForm.getAuthor());
        PageInfo<News> pageInfo = new PageInfo<>(news);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result deleteById(Integer id){
        newsMapper.deleteById(id);
        return new Result(200,"删除成功");
    }

    @Override
    public Result addNews(News news) {
        Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");
        news.setAuthor(admin.getName());
        int i = newsMapper.addNews(news);
        return new Result(200,"添加成功");
    }

    @Override
    public Result passAllnews(News news) {
        Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");
        news.setApprover(admin.getName());
        Date date = new Date();
        news.setApprovetime(date);
//        news.setAdministrator(admin.getName());
        System.out.println(news);
        int i = newsMapper.updatepass(news);
        return new Result(200,"审核成功");
    }

    @Override
    public Result putAwayNews(News news) {
        Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");
        news.setAdministrator(admin.getName());
        System.out.println(news.getAdministrator());
        System.out.println(news);
        int i = newsMapper.updatepass(news);
        System.out.println(i);
        return new Result(200,"上架成功");
    }
}
